mod particle;

use ggez::{Context, GameResult, GameError, graphics, glam::Vec2};

pub struct State{
    particles: Vec<particle::Particle>,
}

impl State{
    pub fn new() -> State {
        let mut particles: Vec<particle::Particle> = vec![];
        for _i in 0..1 {
            let location = Vec2{x: 400.0,y: 10.0};
            let particle = particle::Particle::new(location);
            particles.push(particle);
        }
        State { particles }
    }
}

impl ggez::event::EventHandler<GameError> for State {
    fn update(&mut self, _ctx: &mut Context) -> GameResult {
        for particle in &mut self.particles {
            let fg = Vec2{x: 0.0, y: 0.1} * particle.get_mass();
            particle.apply_force(fg);
            particle.update();
            if particle.is_dead() {
                println!("particle dead!");
            }
        }
        Ok(())
    }
    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::WHITE);
        for particle in &mut self.particles {particle.display(ctx, &mut canvas)}
        canvas.finish(ctx)?;
        Ok(())
    }
}